#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

//patterns  
/* p00 */ const unsigned int p00[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p01 */ const unsigned int p01[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p02 */ const unsigned int p02[] PROGMEM = { 0x40C,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p03 */ const unsigned int p03[] PROGMEM = { 0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p04 */ const unsigned int p04[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x40C,0x4FC,0x418,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p05 */ const unsigned int p05[] PROGMEM = { 0x4E4,0x4FC,0x408,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x408,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x0000 };
/* p06 */ const unsigned int p06[] PROGMEM = { 0x4E4,0x4FC,0x408,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x40C,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x4E4,0x4E4,0x0000 };
/* p07 */ const unsigned int p07[] PROGMEM = { 0x4E4,0x4FC,0x408,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x40C,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x0000 };
/* p08 */ const unsigned int p08[] PROGMEM = { 0x4E4,0x4FC,0x40C,0x4FC,0x4E4,0x4FC,0x4E4,0x4FC,0x408,0x4FC,0x4E4,0x4E4,0x4E4,0x4FC,0x4E4,0x4E4,0x0000 };
/* p09 */ const unsigned int p09[] PROGMEM = { 0x0000 };
/* p0A */ const unsigned int p0A[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p0B */ const unsigned int p0B[] PROGMEM = { 0x0000 };
/* p0C */ const unsigned int p0C[] PROGMEM = { 0x438,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x438,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p0D */ const unsigned int p0D[] PROGMEM = { 0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p0E */ const unsigned int p0E[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p0F */ const unsigned int p0F[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p10 */ const unsigned int p10[] PROGMEM = { 0x40C,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p11 */ const unsigned int p11[] PROGMEM = { 0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x418,0x4FC,0x4FC,0x4FC,0x0000 };
/* p12 */ const unsigned int p12[] PROGMEM = { 0x0000 };
/* p13 */ const unsigned int p13[] PROGMEM = { 0x0000 };
/* p14 */ const unsigned int p14[] PROGMEM = { 0x0000 };
/* p15 */ const unsigned int p15[] PROGMEM = { 0x428,0x4FC,0x42C,0x4FC,0x4FC,0x4FC,0x434,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p16 */ const unsigned int p16[] PROGMEM = { 0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x414,0x4FC,0x418,0x4FC,0x448,0x4FC,0x418,0x4FC,0x448,0x4FC,0x0000 };
/* p17 */ const unsigned int p17[] PROGMEM = { 0x4E4,0x4FC,0x40C,0x4FC,0x4E4,0x414,0x4E4,0x414,0x418,0x4FC,0x4E4,0x418,0x4E4,0x4E4,0x418,0x4E4,0x0000 };
/* p18 */ const unsigned int p18[] PROGMEM = { 0x408,0x408,0x408,0x408,0x408,0x4FC,0x408,0x4FC,0x408,0x408,0x4FC,0x408,0x408,0x4FC,0x408,0x408,0x0000 };
/* p19 */ const unsigned int p19[] PROGMEM = { 0x0000 };
/* p1A */ const unsigned int p1A[] PROGMEM = { 0x4E4,0x4FC,0x438,0x4E4,0x4FC,0x408,0x4E4,0x408,0x438,0x4FC,0x4E4,0x408,0x4E4,0x4FC,0x4E4,0x4FC,0x0000 };
/* p1B */ const unsigned int p1B[] PROGMEM = { 0x0000 };
/* p1C */ const unsigned int p1C[] PROGMEM = { 0x0000 };
/* p1D */ const unsigned int p1D[] PROGMEM = { 0x0000 };
/* p1E */ const unsigned int p1E[] PROGMEM = { 0x0000 };
/* p1F */ const unsigned int p1F[] PROGMEM = { 0x0000 };
/* p20 */ const unsigned int p20[] PROGMEM = { 0x0000 };
/* p21 */ const unsigned int p21[] PROGMEM = { 0x0000 };
/* p22 */ const unsigned int p22[] PROGMEM = { 0x0000 };
/* p23 */ const unsigned int p23[] PROGMEM = { 0x4E4,0x4FC,0x438,0x4E4,0x4FC,0x408,0x4E4,0x40C,0x43C,0x4FC,0x4E4,0x40C,0x4E4,0x4FC,0x4E4,0x4E4,0x0000 };
/* p24 */ const unsigned int p24[] PROGMEM = { 0x4E4,0x4FC,0x43C,0x4E4,0x4FC,0x40C,0x4E4,0x408,0x438,0x4FC,0x4E4,0x408,0x4E4,0x4E4,0x4FC,0x4E4,0x0000 };
/* p25 */ const unsigned int p25[] PROGMEM = { 0x4E4,0x4FC,0x43C,0x4E4,0x4FC,0x414,0x4E4,0x414,0x418,0x448,0x4E4,0x478,0x4E4,0x4A8,0x4E4,0x4E4,0x0000 };
/* p26 */ const unsigned int p26[] PROGMEM = { 0x0000 };
/* p27 */ const unsigned int p27[] PROGMEM = { 0x0000 };
/* p28 */ const unsigned int p28[] PROGMEM = { 0x4E4,0x4FC,0x43C,0x4E4,0x4FC,0x414,0x4E4,0x414,0x418,0x448,0x4E4,0x4D4,0x4E4,0x4A8,0x4E4,0x4E4,0x0000 };
/* p29 */ const unsigned int p29[] PROGMEM = { 0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p2A */ const unsigned int p2A[] PROGMEM = { 0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p2B */ const unsigned int p2B[] PROGMEM = { 0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p2C */ const unsigned int p2C[] PROGMEM = { 0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x438,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p2D */ const unsigned int p2D[] PROGMEM = { 0x4FC,0x4FC,0x408,0x408,0x4FC,0x4FC,0x408,0x4FC,0x408,0x408,0x4FC,0x4FC,0x4FC,0x408,0x408,0x4FC,0x0000 };
/* p2E */ const unsigned int p2E[] PROGMEM = { 0x0000 };
/* p2F */ const unsigned int p2F[] PROGMEM = { 0x0000 };
/* p30 */ const unsigned int p30[] PROGMEM = { 0x0000 };
/* p31 */ const unsigned int p31[] PROGMEM = { 0x0000 };
/* p32 */ const unsigned int p32[] PROGMEM = { 0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x414,0x4FC,0x418,0x4FC,0x4FC,0x4FC,0x4E4,0x4FC,0x4E4,0x4E4,0x0000 };
/* p33 */ const unsigned int p33[] PROGMEM = { 0x0000 };
/* p34 */ const unsigned int p34[] PROGMEM = { 0x408,0x4FC,0x408,0x408,0x4FC,0x408,0x408,0x4FC,0x408,0x4FC,0x408,0x408,0x408,0x4FC,0x408,0x4FC,0x0000 };
/* p35 */ const unsigned int p35[] PROGMEM = { 0x0000 };
/* p36 */ const unsigned int p36[] PROGMEM = { 0x0000 };
/* p37 */ const unsigned int p37[] PROGMEM = { 0x0000 };
/* p38 */ const unsigned int p38[] PROGMEM = { 0x408,0x4FC,0x408,0x408,0x4FC,0x408,0x408,0x4FC,0x408,0x4FC,0x408,0x408,0x408,0x4FC,0x408,0x4FC,0x0000 };
/* p39 */ const unsigned int p39[] PROGMEM = { 0x0000 };
/* p3A */ const unsigned int p3A[] PROGMEM = { 0x0000 };
/* p3B */ const unsigned int p3B[] PROGMEM = { 0x0000 };
/* p3C */ const unsigned int p3C[] PROGMEM = { 0x0000 };
/* p3D */ const unsigned int p3D[] PROGMEM = { 0x0000 };
/* p3E */ const unsigned int p3E[] PROGMEM = { 0x0000 };
/* p3F */ const unsigned int p3F[] PROGMEM = { 0x0000 };
/* p40 */ const unsigned int p40[] PROGMEM = { 0x0000 };
/* p41 */ const unsigned int p41[] PROGMEM = { 0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4A8,0x0000 };
/* p42 */ const unsigned int p42[] PROGMEM = { 0x0000 };
/* p43 */ const unsigned int p43[] PROGMEM = { 0x0000 };
/* p44 */ const unsigned int p44[] PROGMEM = { 0x0000 };
/* p45 */ const unsigned int p45[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p46 */ const unsigned int p46[] PROGMEM = { 0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p47 */ const unsigned int p47[] PROGMEM = { 0x0000 };
/* p48 */ const unsigned int p48[] PROGMEM = { 0x0000 };
/* p49 */ const unsigned int p49[] PROGMEM = { 0x0000 };
/* p4A */ const unsigned int p4A[] PROGMEM = { 0x0000 };
/* p4B */ const unsigned int p4B[] PROGMEM = { 0x0000 };
/* p4C */ const unsigned int p4C[] PROGMEM = { 0x0000 };
/* p4D */ const unsigned int p4D[] PROGMEM = { 0x0000 };
/* p4E */ const unsigned int p4E[] PROGMEM = { 0x0000 };
/* p4F */ const unsigned int p4F[] PROGMEM = { 0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x408,0x4FC,0x408,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };
/* p50 */ const unsigned int p50[] PROGMEM = { 0x0000 };
/* p51 */ const unsigned int p51[] PROGMEM = { 0x0000 };
/* p52 */ const unsigned int p52[] PROGMEM = { 0x0000 };
/* p53 */ const unsigned int p53[] PROGMEM = { 0x0000 };
/* p54 */ const unsigned int p54[] PROGMEM = { 0x0000 };
/* p55 */ const unsigned int p55[] PROGMEM = { 0x408,0x4FC,0x40C,0x4FC,0x4FC,0x4FC,0x414,0x4FC,0x418,0x4FC,0x4FC,0x4FC,0x4E4,0x4FC,0x4E4,0x4E4,0x0000 };
/* p56 */ const unsigned int p56[] PROGMEM = { 0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x468,0x4FC,0x4FC,0x4FC,0x0000 };
/* p57 */ const unsigned int p57[] PROGMEM = { 0x0000 };
/* p58 */ const unsigned int p58[] PROGMEM = { 0x0000 };
/* p59 */ const unsigned int p59[] PROGMEM = { 0x0000 };
/* p5A */ const unsigned int p5A[] PROGMEM = { 0x0000 };
/* p5B */ const unsigned int p5B[] PROGMEM = { 0x0000 };
/* p5C */ const unsigned int p5C[] PROGMEM = { 0x0000 };
/* p5D */ const unsigned int p5D[] PROGMEM = { 0x0000 };
/* p5E */ const unsigned int p5E[] PROGMEM = { 0x0000 };
/* p5F */ const unsigned int p5F[] PROGMEM = { 0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x4FC,0x0000 };


//pattern set  
const uint16_t* const patternSet[] PROGMEM = {p00,p01,p02,p03,p04,p05,p06,p07,p08,p09,p0A,p0B,p0C,p0D,p0E,p0F,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p1A,p1B,p1C,p1D,p1E,p1F,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p2A,p2B,p2C,p2D,p2E,p2F,p30,p31,p32,p33,p34,p35,p36,p37,p38,p39,p3A,p3B,p3C,p3D,p3E,p3F,p40,p41,p42,p43,p44,p45,p46,p47,p48,p49,p4A,p4B,p4C,p4D,p4E,p4F,p50,p51,p52,p53,p54,p55,p56,p57,p58,p59,p5A,p5B,p5C,p5D,p5E,p5F};  

//tracks  
const unsigned int track1[] PROGMEM = {0x05F,0x05F,0x05F,0x05F,0x05F,0x05F,0x05F,0x05F,0x02D,0x038,0x02D,0x038,0x02D,0x038,0x02D,0x034,0xFFFF};  
const unsigned int track2[] PROGMEM = {0x04F,0x055,0x04F,0x055,0x000,0x001,0x002,0x003,0x000,0x001,0x002,0x003,0x000,0x001,0x002,0x032,0xFFFF};  
const unsigned int track3[] PROGMEM = {0x05F,0x05F,0x05F,0x056,0x000,0x001,0x002,0x003,0x000,0x001,0x002,0x003,0x000,0x001,0x002,0x003,0xFFFF};  
const unsigned int track4[] PROGMEM = {0x05F,0x05F,0x05F,0x05F,0x05F,0x05F,0x05F,0x041,0x05F,0x05F,0x05F,0x041,0x045,0x046,0x010,0x016,0xFFFF};  

///////////////////////////////////// SETUP
void setup() {
	Serial.begin(115200);
	gb.begin();
	gb.titleScreen(F("Sound demo"));
}

void loop(){
	if(gb.update()){
		if(gb.buttons.pressed(BTN_A)){
			gb.sound.changePatternSet(patternSet,0);
			gb.sound.changePatternSet(patternSet,1);
			gb.sound.changePatternSet(patternSet,2);
			gb.sound.changePatternSet(patternSet,3);
			gb.sound.playTrack(track1,0);
			gb.sound.playTrack(track2,1);
			gb.sound.playTrack(track3,2);
			gb.sound.playTrack(track4,3);
                        gb.sound.command(CMD_INSTRUMENT, 1, 0, 3); //play the channel 3 with noise
		}
		if(gb.buttons.pressed(BTN_B)){
			gb.sound.playCancel();
		}
		if(gb.buttons.pressed(BTN_C)){
			gb.sound.playCancel();
			gb.titleScreen(F("Sound demo"));
		}
	}
}
